package com.yj.rselasticsearch.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.common.constant.DataConstants;
import com.yj.common.constant.DataScopeConstants;
import com.yj.common.constant.HttpStatus;
import com.yj.common.core.domain.entity.*;
import com.yj.common.core.redis.RedisCache;
import com.yj.common.exception.ServiceException;
import com.yj.common.utils.FastUtils;
import com.yj.common.utils.SecurityUtils;
import com.yj.common.utils.StringUtils;
import com.yj.rselasticsearch.domain.dto.RetrievalRecordDto;
import com.yj.rselasticsearch.domain.dto.WarningInfoDto;
import com.yj.rselasticsearch.domain.vo.MemberVo;
import com.yj.rselasticsearch.service.*;
import com.yj.system.domain.SysRoleDept;
import com.yj.system.mapper.SysRoleDeptMapper;
import com.yj.system.mapper.SysUserMapper;
import com.yj.system.service.ISysUserService;
import com.yj.system.service.impl.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ElasticsearchServiceImpl implements ElasticsearchService {
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private RetrievalRecordService retrievalRecordService;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private WarningInfoService warningInfoService;

    @Resource
    private WhiteListService whiteListService;

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @Resource
    private TokenService tokenService;

    @Resource
    private SysRoleDeptMapper deptMapper;

    @Resource
    private FileTypeService fileTypeService;

    @Resource
    private FileInfoService fileInfoService;


    /**
     * 创建索引
     *
     * @param classType
     * @return
     */
    @Override
    public boolean createIndexAndMapping(Class<?> classType) {
        if (elasticsearchRestTemplate.indexOps(classType).exists()) {
            elasticsearchRestTemplate.indexOps(classType).delete();
        }
        boolean index = elasticsearchRestTemplate.indexOps(classType).create();
        boolean mapping = elasticsearchRestTemplate.indexOps(classType).putMapping();
        return index && mapping;
    }

    /**
     * 删除所有
     *
     * @param classType
     */
    @Override
    public boolean deleteIndex(Class<?> classType) {
        boolean result = true;
        if (elasticsearchRestTemplate.indexOps(classType).exists()) {
            result = elasticsearchRestTemplate.indexOps(classType).delete();
        }
        return result;
    }

    /**
     * 新增数据
     *
     * @param warningInfo
     */
    @Override
    public boolean insert(List<WarningInfo> warningInfo) {
        if (!elasticsearchRestTemplate.indexOps(WarningInfo.class).exists()) {
            log.info("索引不存在！");
            return false;
        }
        try {
            // 批量导入es库。
            BulkRequest bulkRequest = new BulkRequest();
            bulkRequest.timeout("10s");

            // 批处理请求。
            for (int i = 0; i < warningInfo.size(); i++) {
                bulkRequest.add(
                        new IndexRequest("warninginfo")
                                .id(warningInfo.get(i).getId().toString())
                                .source(JSON.toJSONString(warningInfo.get(i)), XContentType.JSON)
                );
            }
            BulkResponse bulkResp = client.bulk(bulkRequest, RequestOptions.DEFAULT);
            System.err.println(bulkResp.hasFailures()); // 是否失败，返回false 代表成功。
            return !bulkResp.hasFailures();
        } catch (Exception exception) {
            throw new ServiceException(exception.getMessage());
        }
    }

    /**
     * 修改数据
     *
     * @param warningInfo
     * @return
     */
    @Override
    public boolean update(WarningInfo warningInfo) {
        WarningInfo warningInfo1 = warningInfoService.selectByPrimaryKey(warningInfo.getId());
        try {
            delete(warningInfo);
            List<WarningInfo> warningInfoList = new LinkedList<>();
            warningInfoList.add(warningInfo1);
            boolean result = insert(warningInfoList);
            log.info(String.format("数据更新结果：%s", result));
            return result;
        } catch (Exception e) {
            log.error(String.format("数据更新失败:%s", JSON.toJSONString(warningInfo1)));
            log.error(e.getMessage(), e);
            return false;
        }

    }

    /**
     * 根据id删除数据
     *
     * @param warningInfo
     * @return
     */
    @Override
    public boolean delete(WarningInfo warningInfo) {
        try {
            Integer deleteId = warningInfo.getId();
            String delete = elasticsearchRestTemplate.delete(deleteId.toString(), WarningInfo.class);
            log.info("删除结果: {}", delete);
            return true;
        } catch (Exception ex) {
            log.info(ex.getMessage());
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 删除指定索引下所有数据
     *
     * @param classType
     */
    @Override
    public void deleteBatch(Class<?> classType) {
        elasticsearchRestTemplate.delete(Query.findAll(), classType, elasticsearchRestTemplate.getIndexCoordinatesFor(classType));
    }


    /**
     * 分词高亮搜索
     *
     * @param warningInfoDto
     * @return
     */
    @Override
    public IPage<WarningInfo> queryHighLightWord(WarningInfoDto warningInfoDto, HttpServletRequest request) {
        MemberVo user = (MemberVo) tokenService.getLoginUser(request);
        if (FastUtils.checkNull(user)) {
            throw new ServiceException("未获取到登录信息，请登录！", HttpStatus.UNAUTHORIZED);
        } else {
            if (FastUtils.checkNull(user.getPhone())) {
                throw new ServiceException("未获取到登录手机号，请重新登录！", HttpStatus.UNAUTHORIZED);
            }
            WhiteList whiteList = whiteListService.getOne(new LambdaQueryWrapper<WhiteList>().eq(WhiteList::getPhone, user.getPhone()).eq(WhiteList::getStatus, DataConstants.Number.ZERO));
            if (FastUtils.checkNull(whiteList)) {
                throw new ServiceException("暂无权限，请联系管理员添加！", HttpStatus.UNAUTHORIZED);
            }
        }
        //先将搜索条件记录在搜索信息日志中
        RetrievalRecordDto retrievalRecordDto = new RetrievalRecordDto();
        retrievalRecordDto.setKeyword(warningInfoDto.getKeyword());
        retrievalRecordDto.setMemberId(user.getId());
        int result = retrievalRecordService.insertSelective(retrievalRecordDto);
        if (result < 1) {
            throw new ServiceException("搜索信息记录异常");
        }
        //分页
        Pageable pageable = PageRequest.of(warningInfoDto.getPageIndex() - 1, warningInfoDto.getPageSize());
        //需要查询的字段
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchBoolPrefixQuery("question", warningInfoDto.getKeyword()));
        //构建高亮查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withHighlightFields(
                        new HighlightBuilder.Field("question"))
                .withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>"))
                .withPageable(pageable)
                .build();
        //查询
        SearchHits<WarningInfo> search = elasticsearchRestTemplate.search(searchQuery, WarningInfo.class);
        //得到查询返回的内容

        //设置一个最后需要返回的实体类集合
        List<WarningInfo> warningInfoList = new ArrayList<>();
        //遍历返回的内容进行处理
        for (org.springframework.data.elasticsearch.core.SearchHit<WarningInfo> searchHit : search.getSearchHits()) {
            //高亮的内容
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            //将高亮的内容填充到content中
            searchHit.getContent().setQuestion(highlightFields.get("question") == null ? searchHit.getContent().getQuestion() : highlightFields.get("question").get(0));
            //放到实体类中
            warningInfoList.add(searchHit.getContent());
        }
        IPage<WarningInfo> warningInfoIPage = new Page<>();
        warningInfoIPage.setTotal(search.getTotalHits());
        warningInfoIPage.setRecords(warningInfoList);
        warningInfoIPage.setCurrent(warningInfoDto.getPageIndex());
        warningInfoIPage.setSize(warningInfoDto.getPageSize());
        warningInfoIPage.setPages(warningInfoIPage.getTotal() % warningInfoDto.getPageSize());
        return warningInfoIPage;
    }

    /**
     * 告警关键词联想
     *
     * @param warningInfoDto
     * @return
     */
    @Override
    public List<String> getAssociationalWord(WarningInfoDto warningInfoDto, HttpServletRequest request) {
        MemberVo user = (MemberVo) tokenService.getLoginUser(request);
        if (FastUtils.checkNull(user)) {
            throw new ServiceException("未获取到登录信息，请登录！", HttpStatus.UNAUTHORIZED);
        } else {
            if (FastUtils.checkNull(user.getPhone())) {
                throw new ServiceException("未获取到登录手机号，请重新登录！", HttpStatus.UNAUTHORIZED);
            }
            SysUser sysUser = sysUserService.selectUserAuthByPhone(user.getPhone());
            if (FastUtils.checkNull(sysUser)) {
                throw new ServiceException("权限不足，请联系管理员！", HttpStatus.UNAUTHORIZED);
            } else {
                if (DataConstants.Number.ONE.equals(sysUser.getIsMiniProgram())) {
                    throw new ServiceException("您当前没有权限访问此应用！", HttpStatus.UNAUTHORIZED);
                }
            }
        }
        //需要查询的字段
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchBoolPrefixQuery("question", warningInfoDto.getKeyword()));
        //构建高亮查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withHighlightFields(
                        new HighlightBuilder.Field("question"))
                .withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>"))
                .build();
        //查询
        SearchHits<WarningInfo> search = null;
        try {
            search = elasticsearchRestTemplate.search(searchQuery, WarningInfo.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ServiceException(String.format("操作错误，请联系管理员！%s", ex.getMessage()));
        }
        //设置一个最后需要返回的实体类集合
        List<String> resultList = new LinkedList<>();
        //遍历返回的内容进行处理
        for (org.springframework.data.elasticsearch.core.SearchHit<WarningInfo> searchHit : search.getSearchHits()) {
            //高亮的内容
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            //将高亮的内容填充到content中
            searchHit.getContent().setQuestion(highlightFields.get("question") == null ? searchHit.getContent().getQuestion() : highlightFields.get("question").get(0));
            resultList.add(searchHit.getContent().getQuestion());
        }
        //list去重
        List<String> newResult = null;
        if (!FastUtils.checkNullOrEmpty(resultList)) {
            if (resultList.size() > 9) {
                newResult = resultList.stream().distinct().collect(Collectors.toList()).subList(0, 9);
            } else {
                newResult = resultList.stream().distinct().collect(Collectors.toList());
            }
        }
        return newResult;
    }

    /**
     * 文档信息关键词联想
     *
     * @param warningInfoDto
     * @return
     */
    @Override
    public List<String> getAssociationalWordOther(WarningInfoDto warningInfoDto, HttpServletRequest request) {
        MemberVo user = (MemberVo) tokenService.getLoginUser(request);
        if (FastUtils.checkNull(user)) {
            throw new ServiceException("未获取到登录信息，请登录！", HttpStatus.UNAUTHORIZED);
        } else {
            if (FastUtils.checkNull(user.getPhone())) {
                throw new ServiceException("未获取到登录手机号，请重新登录！", HttpStatus.UNAUTHORIZED);
            }
            SysUser sysUser = sysUserService.selectUserAuthByPhone(user.getPhone());
            if (FastUtils.checkNull(sysUser)) {
                throw new ServiceException("权限不足，请联系管理员！", HttpStatus.UNAUTHORIZED);
            } else {
                if (DataConstants.Number.ONE.equals(sysUser.getIsMiniProgram())) {
                    throw new ServiceException("您当前没有权限访问此应用！", HttpStatus.UNAUTHORIZED);
                }
            }
        }
        //需要查询的字段，分词联想匹配
        BoolQueryBuilder boolQueryBuilder = null;
        //内容标签contentType字段，过滤
        if (!FastUtils.checkNull(warningInfoDto.getFileTypeId())) {
            boolQueryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termsQuery("fileTypeId", warningInfoDto.getFileTypeId()))
                    .should(QueryBuilders.matchBoolPrefixQuery("fileName", warningInfoDto.getKeyword()));
        } else {
            boolQueryBuilder = QueryBuilders.boolQuery()
                    .should(QueryBuilders.matchBoolPrefixQuery("fileName", warningInfoDto.getKeyword()));
        }
        //构建高亮查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withHighlightFields(
                        new HighlightBuilder.Field("fileName")
                )
                .withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>"))
                .build();
        //查询
        SearchHits<FileInfo> search = null;
        try {
            search = elasticsearchRestTemplate.search(searchQuery, FileInfo.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ServiceException(String.format("操作错误，请联系管理员！%s", ex.getMessage()));
        }
        //设置一个最后需要返回的实体类集合
        List<String> resultList = new LinkedList<>();
        //遍历返回的内容进行处理
        for (org.springframework.data.elasticsearch.core.SearchHit<FileInfo> searchHit : search.getSearchHits()) {
            //高亮的内容
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            //将高亮的内容填充到content中
            searchHit.getContent().setFileName(highlightFields.get("fileName") == null ? searchHit.getContent().getFileName() : highlightFields.get("fileName").get(0));
            if (highlightFields.get("fileName") != null) {
                resultList.add(searchHit.getContent().getFileName());
            }
        }
        //list去重
        List<String> newResult = null;
        if (!FastUtils.checkNullOrEmpty(resultList)) {
            if (resultList.size() > 9) {
                newResult = resultList.stream().distinct().collect(Collectors.toList()).subList(0, 9);
            } else {
                newResult = resultList.stream().distinct().collect(Collectors.toList());
            }
        }
        return newResult;
    }

    /**
     * 高亮分词搜索其它类型文档
     *
     * @param warningInfoDto
     * @param request
     * @return
     */
    @Override
    public IPage<FileInfo> queryHighLightWordOther(WarningInfoDto warningInfoDto, HttpServletRequest request) {
        MemberVo user = (MemberVo) tokenService.getLoginUser(request);
        SysUser sysUser = null;
        if (!FastUtils.checkNull(user)) {
            sysUser = sysUserService.selectUserAuthByPhone(user.getPhone());
        } else {
            throw new ServiceException("未获取到登录信息，请登录！", HttpStatus.UNAUTHORIZED);
        }
        List<SysRoleDept> sysRoleDepts = fileTypeService.getRoleDepts(sysUser).getSysRoleDepts();
        List<SysRoleFileType> sysRoleFileTypes = fileTypeService.getRoleDepts(sysUser).getFileTypes();
        //全文检索
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder
                .should(QueryBuilders.matchBoolPrefixQuery("fileName", warningInfoDto.getKeyword()))
                .should(QueryBuilders.matchBoolPrefixQuery("attachment.content", warningInfoDto.getKeyword()));
        //构建高亮查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withHighlightFields(
                        new HighlightBuilder.Field("fileName"), new HighlightBuilder.Field("attachment.content")
                )
                .withHighlightBuilder(new HighlightBuilder().preTags("<span style='color:red'>").postTags("</span>"))
                .build();
        //查询
        SearchHits<FileInfo> search = null;
        try {
            search = elasticsearchRestTemplate.search(searchQuery, FileInfo.class);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ServiceException(String.format("操作错误，请联系管理员！%s", ex.getMessage()));
        }
        //设置一个最后需要返回的实体类集合
        List<FileInfo> resultList1 = new LinkedList<>();
        //遍历返回的内容进行处理
        for (org.springframework.data.elasticsearch.core.SearchHit<FileInfo> searchHit : search.getSearchHits()) {
            //高亮的内容
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            //将高亮的内容填充到content中
            searchHit.getContent().setFileName(highlightFields.get("fileName") == null ? searchHit.getContent().getFileName() : highlightFields.get("fileName").get(0));
            searchHit.getContent().setContent(highlightFields.get("content") == null ? searchHit.getContent().getContent() : highlightFields.get("content").get(0));
            resultList1.add(searchHit.getContent());
        }
        //类型过滤
        List<FileInfo> resultList2 = new LinkedList<>();
        if (!StringUtils.isEmpty(warningInfoDto.getFileTypeId())) {
            resultList1.forEach(data -> {
                if (data.getFileTypeId().toString().equals(warningInfoDto.getFileTypeId())) {
                    resultList2.add(data);
                }
            });
        } else {
            resultList2.addAll(resultList1);
        }
        //数据同步
        List<FileInfo> fileInfoList = fileInfoService.list();
        for (FileInfo fileInfo : resultList2) {
            for (FileInfo info : fileInfoList) {
                if (fileInfo.getId().toString().equals(info.getId().toString())) {
                    fileInfo.setDeptId(info.getDeptId());
                    fileInfo.setIsOpen(info.getIsOpen());
                    fileInfo.setStatus(info.getStatus());
                    break;
                }
            }
        }
        //部门过滤
        List<FileInfo> resultList = new LinkedList<>();
        if (!StringUtils.isEmpty(warningInfoDto.getDeptId())) {
            resultList2.forEach(data -> {
                if (data.getDeptId().toString().equals(warningInfoDto.getDeptId())) {
                    resultList.add(data);
                }
            });
        } else {
            resultList.addAll(resultList2);
        }
        //数据权限过滤
        List<FileInfo> resultListNew = new ArrayList<>();
         String dataScope = fileTypeService.getRoleDepts(sysUser).getDataScope();
        if (!DataScopeConstants.DATASCOPE.DATA_SCOPE_SELF.equals(dataScope) && !DataScopeConstants.DATASCOPE.DATA_SCOPE_ALL.equals(dataScope)) {
            for (FileInfo fileInfo : resultList) {
                //如果没有条件
                if (StringUtils.isEmpty(warningInfoDto.getDeptId()) && StringUtils.isEmpty(warningInfoDto.getFileTypeId())) {
                    if (DataConstants.Number.ZERO.toString().equals(fileInfo.getIsOpen().toString())) {
                        resultListNew.add(fileInfo);
                        continue;
                    }
                }
                if (!FastUtils.checkNullOrEmpty(sysRoleDepts)) {
                    for (SysRoleDept sysRoleDept : sysRoleDepts) {
                        if (!FastUtils.checkNull(warningInfoDto.getDeptId())) {
                            //公开文档类型
                            if (fileInfo.getDeptId().toString().equals(warningInfoDto.getDeptId()) && fileInfo.getIsOpen().equals(DataConstants.Number.ZERO)) {
                                resultListNew.add(fileInfo);
                                break;
                            }
                        } else if (!FastUtils.checkNull(warningInfoDto.getFileTypeId())) {
                            //分类查询，只要是这分类得都算
                            if (fileInfo.getFileTypeId().toString().equals(fileInfo.getFileTypeId().toString())&&fileInfo.getDeptId().toString().equals(sysRoleDept.getDeptId().toString())) {
                                resultListNew.add(fileInfo);
                                break;
                            }
                        } else {
                            //所有
                            if (fileInfo.getDeptId().toString().equals(sysRoleDept.getDeptId().toString()) || fileInfo.getIsOpen().equals(DataConstants.Number.ZERO)) {
                                resultListNew.add(fileInfo);
                                break;
                            }
                        }
                    }
                } else {
                    if (fileInfo.getIsOpen().toString().equals(DataConstants.Number.ZERO.toString())) {
                        resultListNew.add(fileInfo);
                    }
                }
            }
        } else if (DataScopeConstants.DATASCOPE.DATA_SCOPE_SELF.equals(dataScope)) {
            for (FileInfo fileInfo : resultList) {
                if (!FastUtils.checkNull(warningInfoDto.getDeptId())) {
                    //公开文档类型
                    if (fileInfo.getDeptId().toString().equals(fileInfo.getDeptId().toString()) && fileInfo.getIsOpen().equals(DataConstants.Number.ZERO)) {
                        resultListNew.add(fileInfo);
                    }
                } else if (!FastUtils.checkNull(warningInfoDto.getFileTypeId())&&FastUtils.checkNull(warningInfoDto.getDeptId())) {
                    //分类查询，只要是这分类得都算
                    if (fileInfo.getFileTypeId().toString().equals(warningInfoDto.getFileTypeId())) {
                        if (fileInfo.getIsOpen().toString().equals(DataConstants.Number.ZERO.toString())) {
                            resultListNew.add(fileInfo);
                        } else {
                            if (fileInfo.getCreateBy().equals(sysUser.getUserName())) {
                                resultListNew.add(fileInfo);
                            }
                        }
                    }
                }else{
                    if (fileInfo.getIsOpen().toString().equals(DataConstants.Number.ZERO)){
                        resultListNew.add(fileInfo);
                    }else{
                        if(fileInfo.getCreateBy().equals(sysUser.getUserName())){
                            resultListNew.add(fileInfo);
                        }
                    }
                }
            }
        } else {
            for (FileInfo fileInfo : resultList) {
                //如果没有条件
                if (!FastUtils.checkNull(warningInfoDto.getDeptId())) {
                    //公开文档类型
                    if (fileInfo.getDeptId().toString().equals(fileInfo.getDeptId().toString()) && fileInfo.getIsOpen().equals(DataConstants.Number.ZERO)) {
                        resultListNew.add(fileInfo);
                    }
                } else if (!FastUtils.checkNull(warningInfoDto.getFileTypeId())) {
                    //分类查询，只要是这分类得都算
                    if (fileInfo.getFileTypeId().toString().equals(warningInfoDto.getFileTypeId())) {
                        resultListNew.add(fileInfo);
                    }
                }else{
                    resultListNew.add(fileInfo);
                }
            }
        }
        List<FileInfo> fileInfosNew = new ArrayList<>();
        if (FastUtils.checkNull(warningInfoDto.getDeptId())) {
            for (FileInfo info : resultListNew) {
                if (!FastUtils.checkNullOrEmpty(sysRoleFileTypes)) {
                    for (SysRoleFileType sysRoleFileType : sysRoleFileTypes) {
                        if (info.getIsOpen().equals(DataConstants.Number.ONE)) {
                            //非公开
                            if (info.getFileTypeId().toString().equals(sysRoleFileType.getFileTypeId().toString())) {
                                fileInfosNew.add(info);
                                break;
                            }
                        } else {
                            //公开
                            fileInfosNew.add(info);
                            break;
                        }
                    }
                } else {
                    if (info.getIsOpen().toString().equals(DataConstants.Number.ZERO.toString())) {
                        //公开
                        fileInfosNew.add(info);
                    }
                }
            }
        } else {
            fileInfosNew.addAll(resultListNew);
        }

        //数据状态过滤
        List<FileInfo> fileInfos = new ArrayList<>();
        fileInfosNew.forEach(data ->
        {
            if (data.getStatus().toString().equals(DataConstants.Number.ZERO.toString())) {
                fileInfos.add(data);
            }
        });
        IPage<FileInfo> warningInfoIPage = new Page<>();
        warningInfoIPage.setTotal(fileInfos.size());
        warningInfoIPage.setCurrent(warningInfoDto.getPageIndex());
        warningInfoIPage.setSize(warningInfoDto.getPageSize());
        warningInfoIPage.setPages(warningInfoIPage.getTotal() % warningInfoDto.getPageSize());
        int fromIndex = ((int) warningInfoIPage.getCurrent() - 1) * (int) warningInfoIPage.getSize(); //第几行数据开始
        int toIndex = fromIndex + (int) warningInfoIPage.getSize(); //第几行结束
        if (toIndex > (int) warningInfoIPage.getTotal()) {
            toIndex = (int) warningInfoIPage.getTotal();
        }

        List<FileInfo> pageList = fileInfos.subList(fromIndex, toIndex);
        warningInfoIPage.setRecords(pageList);
        return warningInfoIPage;
    }
}
